// 农历库参考：https://github.com/waterbeside/lunisolar
import lunisolar from "lunisolar";

const zodiacMap = new Map([
    ['水瓶座', [[1, 20], [2, 18]]],
    ['双鱼座', [[2, 19], [3, 20]]],
    ['白羊座', [[3, 21], [4, 19]]],
    ['金牛座', [[4, 20], [5, 20]]],
    ['双子座', [[5, 21], [6, 21]]],
    ['巨蟹座', [[6, 22], [7, 22]]],
    ['狮子座', [[7, 23], [8, 22]]],
    ['处女座', [[8, 23], [9, 22]]],
    ['天秤座', [[9, 23], [10, 22]]],
    ['天蝎座', [[10, 23], [11, 21]]],
    ['射手座', [[11, 22], [12, 21]]],
    ['摩羯座', [[12, 22], [1, 19]]]
]);

export default {
    // 将生日转换为Lunar对象
    convertLunarDate: function (birthday, dateType) {
        const birthdayDate = new Date(birthday);
        let lunarBirthday;
        // 公历生日
        if (dateType === "1") {
            lunarBirthday = lunisolar(birthdayDate);
        } else {
            const lunarData = {
                year: birthdayDate.getFullYear(),
                month: birthdayDate.getMonth() + 1,
                day: birthdayDate.getDate()
            };
            lunarBirthday = lunisolar.fromLunar(lunarData);
        }
        return lunarBirthday;
    },
    // 将生日转为公历生日
    convertSolarDate: function (dateType, year, month, date) {
        let nextBirthday;
        // 公历生日
        if (dateType === "1") {
            nextBirthday = new Date(year, month, date);
        }
        // 农历生日
        else {
            const lunarData = {
                year: year,
                month: month + 1,
                day: date
            };
            nextBirthday = lunisolar.fromLunar(lunarData).toDate();
        }
        return nextBirthday;
    },
    // 计算距离生日有几周年
    yearsUntilBirthday: function (birthday, dateType) {
        const birthdayDate = this.convertLunarDate(birthday, dateType).toDate();
        // 获取当前日期
        const currentDate = new Date();

        // 计算年份差
        let yearDifference = currentDate.getFullYear() - birthdayDate.getFullYear();

        // 检查是否已经过了生日
        if (currentDate.getMonth() > birthdayDate.getMonth() ||
            (currentDate.getMonth() === birthdayDate.getMonth() && currentDate.getDate() > birthdayDate.getDate())) {
            // 如果当前日期还没到生日月份或日期，则减去1年
            yearDifference++;
        }
        return yearDifference;
    },
    // 计算距离生日还有多少天
    // dateType 1：公历生日，2：农历生日
    daysUntilBirthday: function (birthday, dateType) {
        const birthdayDate = new Date(birthday);
        const now = new Date();
        const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
        let nextBirthday = this.convertSolarDate(dateType, today.getFullYear(), birthdayDate.getMonth(), birthdayDate.getDate());

        // 如果今年的生日已经过去，则计算明年的生日
        if (nextBirthday < today) {
            nextBirthday = this.convertSolarDate(dateType, today.getFullYear() + 1, birthdayDate.getMonth(), birthdayDate.getDate());
        }
        const diffTime = Math.abs(nextBirthday.getTime() - today.getTime());
        return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
    },
    // 获取生肖
    getChineseZodiac: function (birthday, dateType) {
        return this.convertLunarDate(birthday, dateType).format("cZ");
    },
    // 获取星座
    getConstellation: function (birthday, dateType) {
        const birthdayDate = this.convertLunarDate(birthday, dateType).toDate();
        const month = birthdayDate.getMonth() + 1;
        const day = birthdayDate.getDate();

        for (const [zodiac, dateRanges] of zodiacMap.entries()) {
            const [startMonth, startDay] = dateRanges[0];
            const [endMonth, endDay] = dateRanges[1];

            if ((month === startMonth && day >= startDay) || (month === endMonth && day <= endDay)) {
                return zodiac;
            }
        }
        throw new Error("无法确定星座，请检查日期是否正确");
    }
};
